package cn.antho.basic.config;

import cn.antho.basic.util.AjaxResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class AdviceController {

    @ResponseBody
    @ExceptionHandler
    public AjaxResult processException(Exception e) {
        AjaxResult result;
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
            result = AjaxResult.me().fail(exception.getBindingResult().getFieldError().getDefaultMessage());
        } else if (e instanceof BussinessException) {
            BussinessException be = (BussinessException) e;
            result = AjaxResult.me().fail(be.getMessage());
        } else {
            e.printStackTrace();
            result = AjaxResult.me().fail();
        }
        return result;
    }

}
